package cn.easybuy.web.backend;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.easybuy.entity.Order;
import cn.easybuy.entity.OrderAndProduct;
import cn.easybuy.service.order.OrderService;
import cn.easybuy.service.order.OrderServiceImpl;
import cn.easybuy.service.order.Order_DetailService;
import cn.easybuy.service.order.Order_DetailServiceImpl;
import cn.easybuy.service.user.UserService;
import cn.easybuy.service.user.UserServiceImpl;
import cn.easybuy.utils.OrderPage;

public class OrderServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public OrderServlet() {
		super();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String action = request.getParameter("action");
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		OrderPage page=new OrderPage();
		OrderService os = new OrderServiceImpl();
		Order_DetailService ods= new Order_DetailServiceImpl();
		if(action.equals("UserOrder")){
			String no = request.getParameter("no");
			List<Order>orderList = os.myOrdrs(no.toString());
			request.setAttribute("orderList", orderList);
			request.getRequestDispatcher("Member_Order.jsp").forward(request, response);
		}else if(action.equals("order_detail")){
			String id = request.getParameter("orderId");
			List<OrderAndProduct>orderList = ods.FindOrder_DetailToId(id);
			request.setAttribute("orderList", orderList);
			request.getRequestDispatcher("Member_Order_Detail.jsp").forward(request, response);
		}else if(action.equals("showPage")){
			String pageNo = request.getParameter("pageNo");
			if(pageNo==null){
				pageNo="1";
			}
			page.setPageNo(Integer.parseInt(pageNo));//设置当前页数
			int pageSize = 5;//每页数据量
			page.setPageSize(pageSize); //设置分页数据量
			page.setDataNum(os.getCount());//设置总数据量
			page.setList(os.getOrders(Integer.parseInt(pageNo), pageSize)); //设置当前页数据集合
			request.setAttribute("page", page);//设置作用域
			request.getRequestDispatcher("Member_OrderList.jsp").forward(request, response);//转发
			}else if(action.equals("del")){
				String id = request.getParameter("id");
				int i = os.del(id);
				if(i>0){
					out.print("<script>");
					out.print("alert('删除成功!');");
					out.print("window.location.href='OrderServlet?action=showPage';");
					out.print("</script>");
				}else{
					out.print("<script>");
					out.print("alert('删除失败!');");
					out.print("window.location.href='OrderServlet?action=showPage';");
					out.print("</script>");
				}
			}
		}
	}
